import {ReduxService} from "@/core/redux_service";
import {actions} from "@/core/redux_service";
const {remote} = require("electron");

const CHANGE_WINDOW_MAX = "CHANGE_WINDOW_MAX";
export type CHANGE_WINDOW_MAX_TYPE = typeof CHANGE_WINDOW_MAX;

export interface IWindowService {
    isMaxWindow:boolean
}

export interface IWindowServiceActions {
    type:CHANGE_WINDOW_MAX_TYPE,
    isMaxWindow:boolean
}


export type WindowServiceModify = IWindowServiceActions;

export  class WindowService extends ReduxService{

    private isMaxWindow:boolean = false;

    baseX:number = 0;
    baseY:number = 0;
    x: number = 0;
    y: number = 0;

    private static initState:IWindowService = {
        isMaxWindow: false
    };
    // 最小化
    minWindow(){
        remote.getCurrentWindow().minimize()
    }

    // 关闭窗口 退出应用
    closeWindow(){
        remote.getCurrentWindow().close();
    }

    // 是否最大化
    getIsMaxWindow(){
        return this.isMaxWindow;
    }

    moveTo(e:MouseEvent){
        this.x = e.screenX - this.baseX;
        this.y = e.screenY - this.baseY;
        remote.getCurrentWindow().setPosition(this.x,this.y)
    }

    @actions()
    changeWindowMax():IWindowServiceActions{
        if(remote.getCurrentWindow().isMaximized()){
            remote.getCurrentWindow().unmaximize();
            this.isMaxWindow= false;
            return {
                type:CHANGE_WINDOW_MAX,
                isMaxWindow:false
            }
        }else{
            remote.getCurrentWindow().maximize();
            this.isMaxWindow = true;
            return {
                type:CHANGE_WINDOW_MAX,
                isMaxWindow:true
            }
        }
    }

    static reducers = (state:IWindowService = WindowService.initState, action:WindowServiceModify):IWindowService => {
       if (action.type === "CHANGE_WINDOW_MAX") {
           return {
               ...state,
               isMaxWindow: action.isMaxWindow,
           }
       }
       return state
    }
}
